<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * User account related class
 *
 * User related functions.
 *
 */
class User_lib
{
	/**
	 * Constructor
	 *
	 * Load all account related model.
	 */
	public function __construct()
	{
		$this->CI = &get_instance();
		
		$this->CI->load->model('pf/user/user_id_model');
		$this->CI->load->model('pf/user/user_info_model');
		$this->CI->load->model('pf/user/user_register_model');
		$this->CI->load->model('pf/user/user_reset_pwd_model');
		$this->CI->load->model('pf/user/user_login_session_model');
		$this->CI->load->model('pf/user/user_temp_model');
		$this->CI->load->helper('str');
	}
	
	/**
	 * Check whether this user name is exist or not
	 *
	 * @param	string	username (phone number or email)
	 * @return	integer	user id
	 */
	public function check_account_by_uname($uname)
	{
		$uid = -1;
		if(is_phone_no($uname))	// user name is phone number
		{
			$uid = $this->CI->user_id_model->get_uid_by_phone($uname);
		}
		elseif(is_email($uname)) // user name is email
		{
			$uid = $this->CI->user_id_model->get_uid_by_email($uname);
		}
		
		return $uid;
	}
	
	/**
	 * Reset user's phone
	 *
	 * @param	integer	user id
	 * @param	string	new phone
	 * @return	boolean
	 */
	public function is_registered_phone($uid, $phone)
	{
		if(empty($uid) || empty($phone))
			return false;
	
		$uid1 = $this->CI->user_id_model->get_uid_by_phone($phone);
		if($uid1 > 0)
			return true;
		
		return false;
	}
	
	/**
	 * Check whether user's token is either valid or not
	 *
	 * @param	integer	user id
	 * @param	integer app id
	 * @param	string	token
	 * @return	boolean
	 */
	public function check_user_token($uid, $appid, $token)
	{
		if(empty($uid) || empty($appid) || empty($token))
			return false;
		
		$data = $this->CI->user_login_session_model->get($uid, $appid);
		if(empty($data))
			return false;
		
		if(strcmp($data['login_token'], $token) != 0)
			return false;
		
		$expire_time = $data['expire_t1'];
		
		if($expire_time < time())
			return false;
		
		return true;
	}
	
	/**
	 * Get user information according to user id
	 *
	 * @param	integer	user id
	 * @return	object	user information
	 */
	public function get_user_info($uid)
	{
		if(empty($uid))
			return array();
		
		$user = $this->CI->user_info_model->get($uid);
		if(empty($user))
			return array();
		
		return $user;
	}
	
	/**
	 * Check whether user has set profile
	 *
	 * @param	integer	user id
	 * @return	boolean
	 */
	public function has_set_profile($uid)
	{
		if(empty($uid))
			return false;
		
		$user = $this->CI->user_info_model->get($uid);
		if(empty($user) || empty($user['thumb']))
			return false;
		
		$profiles = $this->CI->config->item('user_profile');
		
		foreach ($profiles as $key=>$profile) {
			if(strcmp($profile, $user['thumb']) == 0)
				return false;
		}
		
		return true;
	}
	
	/**
	 * Set profile regarding sex
	 *
	 * @param	integer	user id
	 * @return	boolean
	 */
	public function set_profile_by_sex($uid)
	{
		if(empty($uid))
			return null;
	
		$user = $this->CI->user_info_model->get($uid);
		if(empty($user))
			return null;
	
		$profiles = $this->CI->config->item('user_profile');
		$this->CI->user_info_model->update($uid, array('thumb' => $profiles[$user['sex']]));
	
		return $profiles[$user['sex']];
	}
	
	/**
	 * Get user basic information according to user id
	 *
	 * @param	integer	user id
	 * @return	object	user basic information
	 */
	public function get_user_basic_info($uid)
	{
		if(empty($uid))
			return false;
	
		$u = $this->CI->user_info_model->get($uid);
		if(empty($u))
			return null;
		
		$user['user_id'] = $u['user_id'];
		$user['phone'] = $u['phone'];
		$user['email'] = $u['email'];
		
		$user['nickname'] = $u['nickname'];
		$user['sex'] = $u['sex'];
		$user['birthday'] = $u['birthday'];
		$user['thumb'] = (false !== strpos($u['thumb'],'res/static/img/default'))?$this->CI->config->item('res_domain').$u['thumb']:get_img($u['thumb'],'S','','thumb');
		return $user;
	}
	
	/**
	 * Reset user's email
	 *
	 * @param	integer	user id
	 * @param	string	new email
	 * @return	boolean
	 */
	public function reset_user_email($uid, $email)
	{
		if(empty($uid) || empty($email))
			return false;
	
		$uid1 = $this->CI->user_id_model->get_uid_by_email($email);
		if($uid1 > 0 && $uid1 != $uid)
			return false;
	
		if($uid1 == $uid)
			return true;
		
		$user = $this->CI->user_info_model->get($uid);
	
		$this->CI->user_id_model->get_db_w()->trans_begin();
		if(!empty($user['email']))
			$this->CI->user_id_model->delete_email_by_uid_trans($user['email'], $uid);
	
		$this->CI->user_id_model->add_uid_by_email_trans($email, $uid);
	
		//alex add
		$user_state = $this->CI->config->item('user_state');
		$state = $user['user_state'] | $user_state['email'];
		
		$this->CI->user_info_model->update_trans($uid, array('email' => $email,'user_state'=>$state));
	
		if ($this->CI->user_info_model->get_db_w()->trans_status() === FALSE)
		{
			$this->CI->user_info_model->get_db_w()->trans_rollback();
			return false;
		}
		else
			$this->CI->user_info_model->get_db_w()->trans_commit();
	
		return true;
	}
	
	/**
	 * Reset user's phone
	 *
	 * @param	integer	user id
	 * @param	string	new phone
	 * @return	boolean
	 */
	public function reset_user_phone($uid, $phone)
	{
		if(empty($uid) || empty($phone))
			return false;
	
		$uid1 = $this->CI->user_id_model->get_uid_by_phone($phone);
		if($uid1 > 0 && $uid1 != $uid)
			return false;
	
		if($uid1 == $uid)
			return true;
	
		$user = $this->CI->user_info_model->get($uid);
	
		$this->CI->user_id_model->get_db_w()->trans_begin();
		if(!empty($user['phone']))
			$this->CI->user_id_model->delete_phone_by_uid_trans($user['phone'], $uid);
	
		$this->CI->user_id_model->add_uid_by_phone_trans($phone, $uid);
		
		//alex add
		$user_state = $this->CI->config->item('user_state');
		$state = $user['user_state'] | $user_state['phone'];
		$this->CI->user_info_model->update_trans($uid, array('phone' => $phone,'user_state'=>$state));
	
		if ($this->CI->user_info_model->get_db_w()->trans_status() === FALSE)
		{
			$this->CI->user_info_model->get_db_w()->trans_rollback();
			return false;
		}
		else
			$this->CI->user_info_model->get_db_w()->trans_commit();
	
		return true;
	}
	
	/**
	 * Reset user password
	 *
	 * @param	integer	user id
	 * @param	string	new password
	 * @return	boolean
	 */
	public function reset_user_pwd($uid, $pwd)
	{
		if(empty($uid) || empty($pwd))
			return false;
		
		$user_w = $this->CI->user_temp_model->get($uid);
		$data = array();
		if(!empty($user_w)){
			$data['pwd'] = sha1($pwd.$user_w['salt']);
			$this->CI->user_temp_model->update($uid, $data);
			
		}else{
			$user = $this->CI->user_info_model->get($uid);
			if(empty($user))
				return false;
			
			$data['pwd'] = sha1($pwd.$user['salt']);
			$this->CI->user_info_model->update($uid, $data);
		}
		return true;
	}
	
	/**
	 * Reset user information regarding user id
	 *
	 * @param	integer	user id
	 * @param	array	new data
	 * @return	boolean
	 */
	public function reset_user_info($uid, $data)
	{
		if(empty($uid) || empty($data))
			return false;
	
		$this->CI->user_info_model->update($uid, $data);
	
		return true;
	}
	
	/**
	 * Check whether it has already send the verify code in the past 15 mins.
	 *
	 * @param	integer phone number
	 * @return	boolean
	 */
	public function has_sent_sms($phone)
	{
		if($phone <= 0)
			return false;
	
		$data = $this->CI->user_register_model->get($phone, 2);
		if(empty($data))
			return true;
	
		foreach($data as $row)
		{
			if($row['reg_status'] == 0 && (time() - $row['sms_t']) < $this->CI->config->item('send_sms_interval'))
			{
				return false;
			}
		}
	
		return true;
	}
	
	/**
	 * Save the record of sending sms, and return the verification code
	 *
	 * @param	integer phone number
	 * @return	integer verification code.
	 */
	public function add_verify_record($phone = 0)
	{
		if($phone <= 0)
			return 0;
	
		$vcode = 66666;//$this->_generate_verify_code();
		 
		$data = $this->CI->user_register_model->add(array('phone' => $phone,
				'vcode' => $vcode,
				'reg_status' => 0,
				'sms_t' => time()));
		return $vcode;
	}
	
	/**
	 * Send verification code
	 *
	 * @param	integer phone number
	 * @param	integer verification code.
	 * @return	boolean
	 */
	public function send_verify_code($phone, $vcode)
	{
		return true;
	}
	
	/**
	 * Check whether the verify code is correct.
	 *
	 * @param	integer phone number
	 * @param	integer verification code
	 * @return	boolean
	 */
	public function check_verify_code($phone, $vcode)
	{
		if($phone <= 0 || $vcode < 10000 || $vcode > 100000)
			return false;
	
		$data = $this->CI->user_register_model->get($phone, 1);
		if(empty($data[0]))
			return false;
		 
		return $vcode == $data[0]['vcode'] ? true : false;
	}
	
	/**
	 * Check user password.
	 *
	 * @param	integer phone number
	 * @param	integer verification code
	 * @return	boolean
	 */
	public function check_user_pwd($uid=0, $pwd=null)
	{
		if(empty($uid) || empty($pwd))
			return false;
		
		$user_w = $this->CI->user_temp_model->get($uid);
		
		if(!empty($user_w)){
			if(strcmp($user_w['pwd'], sha1($pwd.$user_w['salt'])) != 0)
				return false;
				
		}else{
			$user = $this->CI->user_info_model->get($uid);
			if(empty($user))
				return false;
			 
			if(strcmp($user['pwd'], sha1($pwd.$user['salt'])) != 0)
				return false;
		}
		return true;
	}
	
	/**
	 * Save the record of verification result
	 *
	 * @param	integer phone number
	 * @param	integer verification code.
	 * @return	boolean
	 */
	public function done_verify($phone = 0, $vcode = 0)
	{
		if($phone <= 0 && $verify_code < 10000 && $verify_code > 100000)
			return false;
	
		$data = $this->CI->user_register_model->update($phone, $vcode, array('reg_status' => 1));
	
		return true;
	}
	
	/**
	 * Check login token expiration
	 *
	 * This function could get check login token expiration regarding user id and app id.
	 *
	 * @param	integer	user id
	 * @param	integer application id
	 * @return	boolean	login token expired or not
	 */
	public function check_login_token_expire($uid, $appid, $token=null)
	{
		if(!$uid && !appid)
			return false;
		 
		$data = $this->CI->user_login_session_model->get($uid, $appid);
		if(empty($data) || (!empty($token) && strcmp($data['login_token'], $token)))
			return false;
		 
		$expire_time = $data['expire_t1'];
		 
		return $expire_time > time() ? true : false;
	}
	
	/**
	 * Set cookie
	 *
	 * @param	string	key of cookie
	 * @param	string	value of cookie
	 * @return	null
	 */
	public function set_cookie($key, $value)
	{
		$this->CI->load->helper('cookie');
		set_cookie($key, $value, $this->CI->config->item('cookie_expire'),
		$this->CI->config->item('cookie_domain'));
	}
}